package mathgame.gui;

import mathgame.core.GameState;
import mathgame.core.TimeAttackTimer;
import mathgame.ctrl.ControlFactory;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

public class TimeAttackPanel extends GamePanel implements Observer {
	private JButton jAnswerBtn, jBackBtn, jStartGameBtn;
    private JLabel pointsLabel, multiplierLabel, resultLabel;
    public static JLabel jProblem;
    private JTextField answerField;
    private int timerHeight;
    private TimeAttackPanel timeAttackPanel;

    public TimeAttackPanel(){
        timerHeight = 360;
        timeAttackPanel = this;
        controller.getTimeAttackTimer().addObserver(this);
        controller.getGameState().addObserver(this);
        this.setLayout(null);
        icon = createImageIcon("../source/StartPanel3.png", "background");
        jAnswerBtn = new JButton();
        jAnswerBtn.setFont(new Font("monospaced", Font.BOLD, 22));
        jAnswerBtn.setForeground(Color.GREEN);
        jAnswerBtn.setText("ANSWER");
        jBackBtn = new JButton();
        jBackBtn.setFont(new Font("monospaced", Font.BOLD, 20));
        jBackBtn.setText("BACK");
        jStartGameBtn = new JButton();
        jStartGameBtn.setFont(new Font("monospaced", Font.BOLD, 28));
        jStartGameBtn.setForeground(new Color(0,238,0));
        jStartGameBtn.setText("START");
        jProblem = new JLabel();
        jProblem.setFont(new Font("monospaced", Font.BOLD, 20));
        answerField = new JTextField("");
        answerField.setFont(new Font("monospaced", Font.BOLD, 20));
        pointsLabel = new JLabel();
        pointsLabel.setFont(new Font("monospaced", Font.BOLD, 32));
        pointsLabel.setText("Points: 0");
        multiplierLabel = new JLabel();
        multiplierLabel.setFont(new Font("monospaced", Font.BOLD, 28));
        multiplierLabel.setForeground(new Color(99,184,255));
        multiplierLabel.setText("1 x Multiplier");
        resultLabel = new JLabel("");
        resultLabel.setFont(new Font("monospaced", Font.BOLD, 28));

        jBackBtn.addActionListener(actionListener);
        jAnswerBtn.addActionListener(actionListener);
        jStartGameBtn.addActionListener(actionListener);
        answerField.addActionListener(actionListener);

        this.add(jBackBtn);
        this.add(jAnswerBtn);
        this.add(jExitBtn);
        this.add(jHelpBtn);
        this.add(jStartGameBtn);
        this.add(jProblem);
        this.add(answerField);
        this.add(resultLabel);
        this.add(multiplierLabel);
        this.add(pointsLabel);

        Insets insets = this.getInsets();
        int WIDTH_LARGE = 180;
        int HEIGHT_LARGE = 80;
        int WIDTH_SMALL = 150;
        int HEIGHT_SMALL = 40;

        jProblem.setBounds(492 + insets.left, 320 + insets.top, WIDTH_SMALL, HEIGHT_SMALL);
        jAnswerBtn.setBounds(785 + insets.left, 300 + insets.top, WIDTH_LARGE, HEIGHT_LARGE);
        jStartGameBtn.setBounds(555 + insets.left, 195 + insets.top, 390, 275);
        jBackBtn.setBounds(440 + insets.left, 520 + insets.top, WIDTH_SMALL, HEIGHT_SMALL);
        jExitBtn.setBounds(800 + insets.left, 520 + insets.top, WIDTH_SMALL, HEIGHT_SMALL);
        jHelpBtn.setBounds(620 + insets.left, 520 + insets.top, WIDTH_SMALL, HEIGHT_SMALL);
        answerField.setBounds(620 + insets.left, 320 + insets.top, WIDTH_SMALL, HEIGHT_SMALL);
        resultLabel.setBounds(570 + insets.left, 270 + insets.top, WIDTH_LARGE, HEIGHT_SMALL);
        multiplierLabel.setBounds(570 + insets.left, 190 + insets.top, 240, HEIGHT_SMALL);
        pointsLabel.setBounds(570 + insets.left, 230 + insets.top, 240, HEIGHT_SMALL);

        jAnswerBtn.setVisible(false);
        jProblem.setVisible(false);
        answerField.setVisible(false);

        this.setOpaque(false);
    }

    protected void paintComponent(Graphics g){
        g.drawImage(icon.getImage(), 0, 0, null);
        super.paintComponent(g);
        if(timerHeight>150){
            g.setColor(new Color(0,238,0));
        }
        else if(timerHeight<=150 && timerHeight>70){
            g.setColor(new Color(255,255,0));
        }
        else if(timerHeight<=70 && timerHeight>30){
            g.setColor(new Color(255,185,15));
        }
        else {
            g.setColor(new Color(255,0,0));
        }
        g.fillRect(930-timerHeight,50,timerHeight,100);
        g.setColor(Color.BLACK);
        g.drawRect(570, 50, 360, 100);
    }

    ActionListener actionListener = new ActionListener(){

        public void actionPerformed(ActionEvent e){

            if(e.getSource() == jBackBtn){
                jAnswerBtn.setVisible(false);
                jStartGameBtn.setVisible(true);
                jProblem.setVisible(false);
                answerField.setVisible(false);
                controller.changeCard(1);
                controller.getTimeAttackTimer().stopTimer();
                controller.clearProblemLbl();
                timerHeight = 360;
            }
            else if(e.getSource() == jStartGameBtn){
                jStartGameBtn.setVisible(false);
                jAnswerBtn.setVisible(true);
                jProblem.setVisible(true);
                answerField.setVisible(true);
                controller.newTimeAttackTimer();
                controller.getTimeAttackTimer().addObserver(timeAttackPanel);                
                controller.getTimeAttackTimer().startTimer();
                controller.startTimeAttackGame();
            }
            else if(e.getSource() == jAnswerBtn){
                String answer = answerField.getText();
                controller.setAnswer(answer);
                answerField.setText("");
            }
            else if(e.getSource() == answerField){
                String answer = answerField.getText();
                controller.setAnswer(answer);
                answerField.setText("");
            }
        }
    };

    public void update(Observable o, Object arg) {
        if(o instanceof GameState && arg instanceof String[]){
            String[] s = (String[]) arg;
            jProblem.setText(s[0]);
            resultLabel.setText(s[2]);
            multiplierLabel.setText(s[1]);
            pointsLabel.setText(s[3]);
        }
        else if (o instanceof TimeAttackTimer){

            Integer countDown = controller.getTimeAttackTimer().getiterations() - (Integer) arg;
            if(countDown>=0){
                timerHeight = countDown*6;
                repaint();
            }
            if(countDown==0){
                String userName = JOptionPane.showInputDialog("Enter your name here");
                controller.newHighscore(userName);
                controller.changeCard(1);
            }
        }
    }
}
